﻿<%@ Control Language="C#" AutoEventWireup="true"
    Inherits="Pages_Operators_Ascx_ProviderContactPopup" Codebehind="ProviderContactPopup.ascx.cs" %>
<div id="providerContact-dialog" title="Contact Info">
    <table cellspacing="10" class="popup">
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Title:</div>
                    <div class="field">
                        <asp:DropDownList ID="ddlContactGenderInput" runat="server">
                            <asp:ListItem Value="0">Mr</asp:ListItem>
                            <asp:ListItem Value="1">Mrs</asp:ListItem>
                        </asp:DropDownList>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Name:</div>
                    <div class="field">
                        <asp:HiddenField ID="hidContactID" runat="server"></asp:HiddenField>
                        <asp:TextBox ID="txtContactNameInput" runat="server"></asp:TextBox>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Phone:</div>
                    <div class="field">
                        <asp:TextBox ID="txtContactPhoneInput" runat="server"></asp:TextBox></div>
                    -
                    <asp:TextBox ID="txtContactExtensionInput" Width="68px" runat="server"></asp:TextBox>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Cellphone:</div>
                    <div class="field">
                        <asp:TextBox ID="txtContactCellphoneInput" runat="server"></asp:TextBox>
                    </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Email:</div>
                    <div class="field">
                        <asp:TextBox ID="txtContactEmailInput" runat="server"></asp:TextBox></div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Job Title:</div>
                    <div class="field">
                        <asp:TextBox ID="txtJobTitleInput" runat="server"></asp:TextBox></div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Department:</div>
                    <div class="field">
                        <asp:TextBox ID="txtDepartmentInput" runat="server"></asp:TextBox></div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <span id="litNoticePopup">
                        <asp:Literal ID="litNoticePopup" runat="server" />
                    </span>
                </div>
            </td>
        </tr>
    </table>
</div>

<script language="javascript" type="text/javascript">

    function clearContactForm() {
        $("#<%=hidContactID.ClientID %>").removeAttr('index');
        $("#<%=hidContactID.ClientID %>").val('');
        $("#<%=ddlContactGenderInput.ClientID %>").val(-1);
        $("#<%=txtContactNameInput.ClientID %>").val('');
        $("#<%=txtContactPhoneInput.ClientID %>").val('');
        $("#<%=txtContactExtensionInput.ClientID %>").val('');
        $("#<%=txtContactCellphoneInput.ClientID %>").val('');
        $("#<%=txtContactEmailInput.ClientID %>").val('');
        $("#<%=txtJobTitleInput.ClientID %>").val('');
        $("#<%=txtDepartmentInput.ClientID %>").val('');

        $("#<%=txtContactNameInput.ClientID %>").focus();
    }

    function insertContact() {
        var tbody = $('#gridProviderContact > tbody');
        var seq = ($('tr', tbody).length + 1);
        var evenClass = (seq % 2 == 0 ? ' class="evenRow"' : '');
        var buttons = '<a contactid="' + $("#<%=hidContactID.ClientID %>").val() + '" class="btnEdit new">Edit</a>';
        buttons += '|&nbsp;<a contactid="' + $("#<%=hidContactID.ClientID %>").val() + '" class="btnDelete new">Delete</a>';

        try {
            var row = stringformat('<tr{0}><td align="center">{1}</td><td style="display: none"></td><td style="display: none">{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td><td>{11}</td></tr>',
                  evenClass,
                  seq,
                  $("#<%=ddlContactGenderInput.ClientID %> option:selected").val(),
                  $("#<%=ddlContactGenderInput.ClientID %> option:selected").text(),
                  $("#<%=txtContactNameInput.ClientID %>").val(),
                  $("#<%=txtContactPhoneInput.ClientID %>").val(),
                  $("#<%=txtContactExtensionInput.ClientID %>").val(),
                  $("#<%=txtContactCellphoneInput.ClientID %>").val(),
                  $("#<%=txtContactEmailInput.ClientID %>").val(),
                  $("#<%=txtJobTitleInput.ClientID %>").val(),
                  $("#<%=txtDepartmentInput.ClientID %>").val(),
                  buttons);

            if ($.trim(tbody.html()) == '') {
                tbody.html(row);
            }
            else {
                $('tr:last', tbody).after(row);
            }

            setEvents();

            $("#litNoticePopup").text("INSERT SUCCESSFUL.");
        }
        catch (e) {
            $("#litNoticePopup").text("INSERT FAILS.");
        }
    }

    function updateContact() {
        try {
            var line = $("#gridProviderContact > tbody > tr:eq(" + $("#<%=hidContactID.ClientID %>").attr('index') + ")");

            $(line).children("td:eq(2)").text($("#<%=ddlContactGenderInput.ClientID %> option:selected").val() == 0);
            $(line).children("td:eq(3)").text($("#<%=ddlContactGenderInput.ClientID %> option:selected").text());
            $(line).children("td:eq(4)").text($("#<%=txtContactNameInput.ClientID %>").val());
            $(line).children("td:eq(5)").text($("#<%=txtContactPhoneInput.ClientID %>").val());
            $(line).children("td:eq(6)").text($("#<%=txtContactExtensionInput.ClientID %>").val());
            $(line).children("td:eq(7)").text($("#<%=txtContactCellphoneInput.ClientID %>").val());
            $(line).children("td:eq(8)").text($("#<%=txtContactEmailInput.ClientID %>").val());
            $(line).children("td:eq(9)").text($("#<%=txtJobTitleInput.ClientID %>").val());
            $(line).children("td:eq(10)").text($("#<%=txtDepartmentInput.ClientID %>").val());

            $("#providerContact-dialog").dialog("close");

            $("#litNoticePopup").text("UPDATE SUCCESSFUL.");
        }
        catch (e) {
            $("#litNoticePopup").text("UPDATE FAILS.");
        }
    }

    $("#providerContact-dialog").dialog({
        autoOpen: false,
        width: 680,
        modal: true,
        buttons: {
            "OK": function() {
                var isNew = $("#<%=hidContactID.ClientID %>").attr('index') == undefined;
                if (isNew) {
                    insertContact();
                }
                else {
                    updateContact();
                }

            },
            Cancel: function() {
                $(this).dialog("close");
            }
        },
        close: function() {
        }
    });

    function editContact(idx) {
        var line = $("#gridProviderContact > tbody > tr:eq(" + idx + ")");
        $("#<%=hidContactID.ClientID %>").attr('index', idx);
        $("#<%=hidContactID.ClientID %>").val($(line).children("td:eq(1)").text());
        $("#<%=ddlContactGenderInput.ClientID %>").val($(line).children("td:eq(2)").text() == 'true' ? 0 : 1);
        $("#<%=txtContactNameInput.ClientID %>").val($(line).children("td:eq(4)").text());
        $("#<%=txtContactPhoneInput.ClientID %>").val($(line).children("td:eq(5)").text());
        $("#<%=txtContactExtensionInput.ClientID %>").val($(line).children("td:eq(6)").text());
        $("#<%=txtContactCellphoneInput.ClientID %>").val($(line).children("td:eq(7)").text());
        $("#<%=txtContactEmailInput.ClientID %>").val($(line).children("td:eq(8)").text());
        $("#<%=txtJobTitleInput.ClientID %>").val($(line).children("td:eq(9)").text());
        $("#<%=txtDepartmentInput.ClientID %>").val($(line).children("td:eq(10)").text());

        $("#<%=txtContactNameInput.ClientID %>").focus();
    }
</script>

